package com.gzhryc.shared_device.oem.h5.mng.controllers.market.cloud;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.data.dto.SelectResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.json.JsonTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.net_marketing.cloud.business.dao.db.MarketProjectApp;
import com.gzhryc.net_marketing.cloud.business.dao.enums.MarketProjectAppEnum;
import com.gzhryc.net_marketing.cloud.business.services.MarketProjectAppService;
import com.gzhryc.net_marketing.cloud.business.services.dto.MarketProjectAppSearch;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.common.payment.dao.enums.EAppType;
import com.gzhryc.shared_device.oem.h5.mng.WebConstants;

public class MarketProjectAppController {
	
	static Logger log = Logger.getLogger(MarketProjectAppController.class);

    @Route(value = { "/market/cloud/project/list.html" })
    public String list(HttpServletRequest request) {
        request.setAttribute("stateList", MarketProjectAppEnum.State.values());
        return "/market/cloud/project/list.html";
    }

    @Route(value = "/market/cloud/project/list.data")
    public ListResult<MarketProjectApp> listData(HttpServletRequest request) {
        ListResult<MarketProjectApp> result = new ListResult<>(DtoConstants.SUCCESS);

        MarketProjectAppSearch search = WebUtils.getParameter(request, MarketProjectAppSearch.class);
        Integer pageNo = WebUtils.getInteger(request, "page", 1);
        Integer pageLimit = WebUtils.getInteger(request, "limit", WebConstants.PAGE_LIMIT);

        result.getBody().setTotal(MarketProjectAppService.self().countBySearch(search));
        result.getBody().setData(MarketProjectAppService.self().findBySearch(search, pageNo, pageLimit));

        if (result.getBody().getTotal() == 0) {
            result.setCode(DtoConstants.NULL_DATA, "暂无数据");
        }

        return result;
    }

    @Route(value = { "/market/cloud/project/add.html" })
    public String add(HttpServletRequest request) {
        request.setAttribute("appTypeList", EAppType.values());
        return "/market/cloud/project/edit.html";
    }

    @Route(value = { "/market/cloud/project/add.do" })
    public AjaxResult doAdd(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "新增运营商应用失败");
        MarketProjectApp app = WebUtils.getParameter(request, MarketProjectApp.class);
        String phone = WebUtils.getString(request, "phone");
        if (app != null && StringTools.isNotBlank(app.getName()) && StringTools.isNotBlank(phone)) {
            try {
                if (MarketProjectAppService.self().add(app,phone)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = { "/market/cloud/project/info.html" })
    public String cloudInfo(HttpServletRequest request) {
        Long id = WebUtils.getLong(request, "id");
        if (id != null) {
            MarketProjectApp software = MarketProjectAppService.self().getById(id);
            if (software != null) {
                WebUtils.setAttribute(request, software);
                if(StringUtils.isNotBlank(software.getDbConfig())) {
                    Map<String,Object> dbConfig = JsonTools.fromJson(software.getDbConfig());
                    request.setAttribute("dbConfig", dbConfig);
                }
            }
        }
        request.setAttribute("appTypeList", EAppType.values());
        return "/market/cloud/project/info.html";
    }

    @Route(value = { "/market/cloud/project/update.do" })
    public AjaxResult doUpdate(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "修改运营商应用失败");
        MarketProjectApp app = WebUtils.getParameter(request, MarketProjectApp.class);
        if (app != null) {
            try {
                if (MarketProjectAppService.self().updateNotNull(app)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = { "/market/cloud/project/db_config/save.do" })
    public AjaxResult doSaveCloudDbConfig(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "保存应用数据库配置失败");
        Long id = WebUtils.getLong(request, "id");
        Map<String, String> dbConfig = WebUtils.getSingleParameter(request, "dbConfig");
        if (id != null && dbConfig != null) {
            try {
                if (MarketProjectAppService.self().updateDbConfig(id, JsonTools.toJson(dbConfig))) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }
    
    @Route(value = { "/market/cloud/project/state/update.do" })
    public AjaxResult doEnable(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "修改失败");
        Long id = WebUtils.getLong(request, "id");
        if (id != null) {
            try {
                if (MarketProjectAppService.self().toSwitchState(id)) {
                    result.setCode(DtoConstants.SUCCESS, null);
                }
            } catch (LogicException e) {
                result.setMsg(e.getMessage());
            }
        }
        return result;
    }

    @Route(value = { "/market/cloud/project/del.do" })
    public AjaxResult doDel(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "删除运营商应用失败");
        Long id = WebUtils.getLong(request,"id");
        try {
            if (MarketProjectAppService.self().deleteById(id)) {
                result.setCode(DtoConstants.SUCCESS, null);
            }
        } catch (LogicException e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }
    
    @Route("/market/project/select.data")
    public SelectResult selectData(HttpServletRequest request) {
        SelectResult result = new SelectResult(DtoConstants.SUCCESS);
        String term = request.getParameter("term");
        Integer page = WebUtils.getInteger(request, "page", 1);

        MarketProjectAppSearch search = new MarketProjectAppSearch();
        search.setName(term);
        Long total = 0L;
        List<MarketProjectApp> list = MarketProjectAppService.self().findBySearch(search, page, WebConstants.PAGE_LIMIT);
        if (list != null) {
            for (MarketProjectApp user : list) {
                SelectResult.Item item = new SelectResult.Item();
                item.setId(user.getId().toString());
                item.setText(user.getName());
                result.getBody().getData().add(item);
            }
            total += list.size();
        }
        result.getBody().setPage(page);
        result.getBody().setLimit(WebConstants.PAGE_LIMIT);
        result.getBody().setTotal(total);
        return result;
    }
}
